package ar.edu.uba.fi.jmit.graphvisualizer.receiver;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import ar.edu.uba.fi.jmit.graphvisualizer.server.ResourceProperties;
import ar.edu.uba.fi.jmit.integrationcommons.Event;
import ar.edu.uba.fi.jmit.receiver.ReceiverObserver;

public class SecondaryInformationObserverImpl implements ReceiverObserver {
	
	private static SecondaryInformationObserverImpl instance = null;
	private Map<String, Map<String, String>> secInfo = new HashMap<String, Map<String, String>>();
	
	private SecondaryInformationObserverImpl() {
	}
	
	public static SecondaryInformationObserverImpl getInstance() {
		if (instance == null)
			instance = new SecondaryInformationObserverImpl();
		return instance;
	}

	@Override
	public void onRecivedEvent(Event event) {
		String monitorType = (String)event.getData().get(ResourceProperties.getProperty("jmit.graphvisualizer.monitor.monitorType")); 
		if ("METHOD_INVOCATION_COUNTER".equals(monitorType) || "OBJECT_COUNT".equals(monitorType)) {
			String application = (String)event.getData().get(ResourceProperties.getProperty("jmit.graphvisualizer.monitor.clientApplicationName"));
			String monitor = (String)event.getData().get(ResourceProperties.getProperty("jmit.graphvisualizer.key.monitorName"));
			if ("METHOD_INVOCATION_COUNTER".equals(monitorType))
				secInfo.put(application + " - " + monitor, getValuesForMethodInvocationCounter(event.getData()));
			else
				secInfo.put(application + " - " + monitor, getValuesForObjectCounter(event.getData()));			
		}
	}

	private Map<String, String> getValuesForObjectCounter(Map<String, Serializable> data) {
		Map<String, String> values = new LinkedHashMap<String, String>();
		values.put("Created in the last minute", data.get("object_count_minute").toString());
		values.put("Created in the last hour", data.get("object_count_hour").toString());
		values.put("Created in the last day", data.get("object_count_day").toString());
		values.put("Created in the last month", data.get("object_count_month").toString());
		if (data.get("time_interval") != null) {
			Integer seconds = Integer.valueOf(data.get("time_interval").toString());
			if (seconds > 0) {
				values.put("Created in the last " + seconds + " seconds", data.get("object_count_custom").toString());
			}
		}
		
		return values;
	}

	private Map<String, String> getValuesForMethodInvocationCounter(Map<String, Serializable> data) {
		Map<String, String> values = new LinkedHashMap<String, String>();
		values.put("Invocations in the last minute", data.get("method_invocation_minute").toString());
		values.put("Invocations in the last hour", data.get("method_invocation_hour").toString());
		values.put("Invocations in the last day", data.get("method_invocation_day").toString());
		values.put("Invocations in the last month", data.get("method_invocation_month").toString());
		if (data.get("time_interval") != null) {
			Integer seconds = Integer.valueOf(data.get("time_interval").toString());
			if (seconds > 0) {
				values.put("Invocations in the last " + seconds + " seconds", data.get("method_invocation_custom").toString());
			}
		}
		
		return values;
	}

	public Map<String, String> getValues(String appName, String monName) {
		return secInfo.get(appName + " - " + monName);
	}
}
